package com.kang.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.AuditFlow;
import com.kang.pojo.AuditFlowItem;
import com.kang.pojo.Role;
import com.kang.service.AuditFlowItemService;
import com.kang.service.AuditFlowService;
import com.kang.service.RoleService;

/**
 * 审批流程控制层
 * @author wd
 *
 */
@Controller
@RequestMapping("auditFlow")
@SessionAttributes({"auditFlowPage"})
public class AuditFlowController extends com.kang.controller.Controller {

	/**
	 * 工作流程（服务层接口）
	 */
	@Resource
	private AuditFlowService auditFlowService;
	/**
	 * 工作流程--流程级别（服务接口）
	 */
	@Resource
	private AuditFlowItemService auditFlowItemService;
	/**
	 * 角色（服务层接口）
	 */
	@Resource
	private RoleService roleService;

	/**
	 * 查询所有的审批工作流程
	 * @param session	会话
	 * @param model	模型
	 * @param auditFlow	工作流程（实体类）
	 * @return	审批工作流程集合
	 */
	@RequestMapping("listAllFlow")
	public String listAllFlow(HttpSession session,Model model,AuditFlow auditFlowPage){
		//权限判断
		initRight(session);
		if (checkRight("查看审批流程") != SUCCESS) {
			return "basic/noRight";
		}
		
		List<AuditFlow> auditFlowList = auditFlowService.listAllFlow(auditFlowPage);
		model.addAttribute("auditFlowList",auditFlowList);
		
		return "system/approvalFlow/listAuditFlow";
	}
	
	/**
	 * 根据工作流程ID查询流程信息及流程级别
	 * @param session	会话
	 * @param model	模型
	 * @param auditFlowID	工作流程ID
	 * @return	查询相关信息去到编辑审批流程页面
	 */
	@RequestMapping("toEditFlow")
	public String toEditFlow(HttpSession session,Model model,int auditFlowID){
		//权限判断
		initRight(session);
		if (checkRight("查看审批流程") != SUCCESS) {
			return "basic/noRight";
		}
		model.addAttribute("isEdit", checkRight("修改审批流程") == SUCCESS);
		
		//审批流程信息
		AuditFlow auditFlow = auditFlowService.getAuditFlowById(auditFlowID);
		model.addAttribute("auditFlow",auditFlow);
		//查询所有权限（角色）
		List<Role> roleList = roleService.getAllRoleToFlow();//权限管理-获取所有权限角色role
		model.addAttribute("roleList",roleList);
		//查询流程级别list
		List<AuditFlowItem> auditFlowItemList = auditFlowItemService.getAuditFlowItemListByAuditFlowID(auditFlowID);
		model.addAttribute("auditFlowItemList",auditFlowItemList);
		return "system/approvalFlow/editAuditFlow";
	}
	
	/**
	 * 根据角色id查询使用该角色的用户
	 * @param session	会话
	 * @param roleID	角色ID
	 * @return	角色的用户JSON字符串
	 */
	@RequestMapping("queryRoleUserByRoleId")
	@ResponseBody
	public String queryRoleUserByRoleId(HttpSession session,int roleID){
		AuditFlowItem auditFlowItem = auditFlowItemService.queryRoleUserByRoleId(roleID);
		return JSON.toJSONString(auditFlowItem);
	}
	
	/**
	 * 修改审批流程信息及审批级别
	 * @param session	会话
	 * @param auditFlow	审批流程封装的实体类
	 * @param level_S	权限的级别数组
	 * @param roleID_S	角色ID数组
	 * @param passPersonNumber_S	需要通过的人数数组
	 * @return	成功与失败的字符串
	 */
	@RequestMapping("updateEditFlowInfo")
	@ResponseBody
	public String updateEditFlowInfo(HttpSession session,AuditFlow auditFlow,Integer[] level_S,
			Integer[] roleID_S,Integer[] passPersonNumber_S){
		AjaxResult result = new AjaxResult();
		
		//权限判断
		initRight(session);
		if(checkRight("修改审批流程") != SUCCESS){
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		
		auditFlow.setLastPersonID((Integer) session.getAttribute("userID"));//最后修改人id
		int a=auditFlowService.updateAuditFlowById(auditFlow);
		if(a>0){
			auditFlowItemService.deleteAuditFlowItemForAuditFlowId(auditFlow.getAuditFlowID());
			auditFlowItemService.addAuditFlowItemToAuditFlow(auditFlow.getAuditFlowID(),level_S,roleID_S,passPersonNumber_S);
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}
	
}
